#ifndef LABEL_LIST_WIDGET_H
#define LABEL_LIST_WIDGET_H

#include <QApplication>
#include <QAbstractTextDocumentLayout>
#include <QListView>
#include <QPainter>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QStyleOptionViewItem>
#include <QTextDocument>
#include <QWidget>
#include "../shape.h"


class HTMLDelegate : public QStyledItemDelegate
{
public:
	HTMLDelegate(QObject* parent = nullptr);

	void paint(QPainter* painter, const QStyleOptionViewItem& option,
	           const QModelIndex& index) const override;

	QSize sizeHint(const QStyleOptionViewItem& option,
	               const QModelIndex& index) const override;
};


class LabelListWidgetItem : public QStandardItem
{
public:
	LabelListWidgetItem(const QString& text = QString(), ::Shape* shape = nullptr);

	QStandardItem* clone() const override;

	void setShape(::Shape* shape);

	::Shape* shape() const;
};


class StandardItemModel : public QStandardItemModel
{
	Q_OBJECT

public:
	using QStandardItemModel::QStandardItemModel;

signals:
	void itemDropped();

protected:
	bool removeRows(int row, int count,
	                const QModelIndex& parent = QModelIndex()) override;
};

class LabelListWidget : public QListView
{
	Q_OBJECT

public:
	LabelListWidget(QWidget* parent = nullptr);

	int size() const;

	LabelListWidgetItem* at(int i) const;

	QList<LabelListWidgetItem*> items() const;

signals:
	void itemDoubleClicked(LabelListWidgetItem* item);
	void itemSelectionChanged(const QList<LabelListWidgetItem*>& selected,
	                          const QList<LabelListWidgetItem*>& deselected);

private slots:
	void itemDoubleClickedEvent(const QModelIndex& index);

	void itemSelectionChangedEvent(const QItemSelection& selected,
	                               const QItemSelection& deselected);

public:
	QList<LabelListWidgetItem*> selectedItems() const;

	void scrollToItem(LabelListWidgetItem* item);

	void addItem(LabelListWidgetItem* item);

	void removeItem(LabelListWidgetItem* item);

	void selectItem(LabelListWidgetItem* item);

	LabelListWidgetItem* findItemByShape(::Shape* shape);

	void clear();
};


#endif // LABEL_LIST_WIDGET_H
